using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.SupplyChain.Warehouse;
using HengTong.Model;
using HengTong.Model._Admin;
using TiaoTaoHelper.Wtm.Models;


namespace HengTong.ViewModel.SupplyChain.SysWarehouseVms
{
    public partial class SysWarehouseListVm : BaseListVm<SysWarehouseView, SysWarehouseSearcher>
    {

        protected override IEnumerable<IGridColumn<SysWarehouseView>> InitGridHeader()
        {
            return new List<GridColumn<SysWarehouseView>>{
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Code),
                this.MakeGridHeader(x => x.ErpId),
                this.MakeGridHeader(x => x.Description),
                this.MakeGridHeader(x => x.Location),
                this.MakeGridHeader(x => x.IsVirtual),
                this.MakeGridHeader(x => x.CommonState),
                this.MakeGridHeader(x => x.StockersName),
                this.MakeGridHeader(x => x.OrganizationsName),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysWarehouseView> GetSearchQuery()
        {
            var query = DC.Set<SysWarehouse>()
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Code, x=>x.Code)
                .CheckContain(Searcher.ErpId, x=>x.ErpId)
                .CheckEqual(Searcher.IsVirtual, x=>x.IsVirtual)
                .CheckEqual(Searcher.CommonState, x=>x.CommonState)
                .Select(x => new SysWarehouseView
                {
				    ID = x.ID,
                    Name = x.Name,
                    Code = x.Code,
                    ErpId = x.ErpId,
                    Description = x.Description,
                    Location = x.Location,
                    IsVirtual = x.IsVirtual,
                    CommonState = x.CommonState,
                    StockersName = x.Stockers.Select(y=>y.Stocker.Name).ToSepratedString(null,",")
                })
                .OrderBy(x => x.CommonState)
                .ThenBy(x => x.Code);
            return query;
        }
    }

    public class SysWarehouseView : SysWarehouse{
        [Display(Name = "_Admin.Name")]
        public String StockersName { get; set; }
        [Display(Name = "组织名称")]
        public String OrganizationsName { get; set; }

    }
}
